1   /*
2    * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions
6    * are met:
7    *
8    *   - Redistributions of source code must retain the above copyright
9    *     notice, this list of conditions and the following disclaimer.
10   *
11   *   - Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in the
13   *     documentation and/or other materials provided with the distribution.
14   *
15   *   - Neither the name of Oracle nor the names of its
16   *     contributors may be used to endorse or promote products derived
17   *     from this software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  
32  
33  import java.applet.Applet;
34  import java.awt.Color;
35  import java.awt.Font;
36  import java.awt.Graphics;
37  import java.text.SimpleDateFormat;
38  import java.util.Date;
39  import java.util.Locale;
40  
41  
42  /**
43   * Time!
44   *
45   * @author Rachel Gollub
46   * @author Daniel Peek replaced circle drawing calculation, few more changes
47   */
48  @SuppressWarnings("serial")
49  public class Clock extends Applet implements Runnable {
50  
51      private volatile Thread timer;       // The thread that displays clock
52      private int lastxs, lastys, lastxm,
53              lastym, lastxh, lastyh;  // Dimensions used to draw hands
54      private SimpleDateFormat formatter;  // Formats the date displayed
55      private String lastdate;             // String to hold date displayed
56      private Font clockFaceFont;          // Font for number display on clock
57      private Date currentDate;            // Used to get date to display
58      private Color handColor;             // Color of main hands and dial
59      private Color numberColor;           // Color of second hand and numbers
60      private int xcenter = 80, ycenter = 55; // Center position
61  
62      @Override
63      public void init() {
64          lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
65          formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss yyyy",
66                  Locale.getDefault());
67          currentDate = new Date();
68          lastdate = formatter.format(currentDate);
69          clockFaceFont = new Font("Serif", Font.PLAIN, 14);
70          handColor = Color.blue;
71          numberColor = Color.darkGray;
72  
73          try {
74              setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),
75                      16)));
76          } catch (NullPointerException e) {
77          } catch (NumberFormatException e) {
78          }
79          try {
80              handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),
81                      16));
82          } catch (NullPointerException e) {
83          } catch (NumberFormatException e) {
84          }
85          try {
86              numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),
87                      16));
88          } catch (NullPointerException e) {
89          } catch (NumberFormatException e) {
90          }
91          resize(300, 300);              // Set clock window size
92      }
93  
94      /**
95       * Paint is the main part of the program
96       */
97      @Override
98      public void update(Graphics g) {
99          int xh, yh, xm, ym, xs, ys;
100         int s = 0, m = 10, h = 10;
101         String today;
102 
103         currentDate = new Date();
104 
105         formatter.applyPattern("s");
106         try {
107             s = Integer.parseInt(formatter.format(currentDate));
108         } catch (NumberFormatException n) {
109             s = 0;
110         }
111         formatter.applyPattern("m");
112         try {
113             m = Integer.parseInt(formatter.format(currentDate));
114         } catch (NumberFormatException n) {
115             m = 10;
116         }
117         formatter.applyPattern("h");
118         try {
119             h = Integer.parseInt(formatter.format(currentDate));
120         } catch (NumberFormatException n) {
121             h = 10;
122         }
123 
124         // Set position of the ends of the hands
125         xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter);
126         ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter);
127         xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter);
128         ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter);
129         xh = (int) (Math.cos((h * 30 + m / 2) * Math.PI / 180 - Math.PI / 2)
130                 * 30
131                 + xcenter);
132         yh = (int) (Math.sin((h * 30 + m / 2) * Math.PI / 180 - Math.PI / 2)
133                 * 30
134                 + ycenter);
135 
136         // Get the date to print at the bottom
137         formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
138         today = formatter.format(currentDate);
139 
140         g.setFont(clockFaceFont);
141         // Erase if necessary
142         g.setColor(getBackground());
143         if (xs != lastxs || ys != lastys) {
144             g.drawLine(xcenter, ycenter, lastxs, lastys);
145             g.drawString(lastdate, 5, 125);
146         }
147         if (xm != lastxm || ym != lastym) {
148             g.drawLine(xcenter, ycenter - 1, lastxm, lastym);
149             g.drawLine(xcenter - 1, ycenter, lastxm, lastym);
150         }
151         if (xh != lastxh || yh != lastyh) {
152             g.drawLine(xcenter, ycenter - 1, lastxh, lastyh);
153             g.drawLine(xcenter - 1, ycenter, lastxh, lastyh);
154         }
155 
156         // Draw date and hands
157         g.setColor(numberColor);
158         g.drawString(today, 5, 125);
159         g.drawLine(xcenter, ycenter, xs, ys);
160         g.setColor(handColor);
161         g.drawLine(xcenter, ycenter - 1, xm, ym);
162         g.drawLine(xcenter - 1, ycenter, xm, ym);
163         g.drawLine(xcenter, ycenter - 1, xh, yh);
164         g.drawLine(xcenter - 1, ycenter, xh, yh);
165         lastxs = xs;
166         lastys = ys;
167         lastxm = xm;
168         lastym = ym;
169         lastxh = xh;
170         lastyh = yh;
171         lastdate = today;
172         currentDate = null;
173     }
174 
175     @Override
176     public void paint(Graphics g) {
177         g.setFont(clockFaceFont);
178         // Draw the circle and numbers
179         g.setColor(handColor);
180         g.drawArc(xcenter - 50, ycenter - 50, 100, 100, 0, 360);
181         g.setColor(numberColor);
182         g.drawString("9", xcenter - 45, ycenter + 3);
183         g.drawString("3", xcenter + 40, ycenter + 3);
184         g.drawString("12", xcenter - 5, ycenter - 37);
185         g.drawString("6", xcenter - 3, ycenter + 45);
186 
187         // Draw date and hands
188         g.setColor(numberColor);
189         g.drawString(lastdate, 5, 125);
190         g.drawLine(xcenter, ycenter, lastxs, lastys);
191         g.setColor(handColor);
192         g.drawLine(xcenter, ycenter - 1, lastxm, lastym);
193         g.drawLine(xcenter - 1, ycenter, lastxm, lastym);
194         g.drawLine(xcenter, ycenter - 1, lastxh, lastyh);
195         g.drawLine(xcenter - 1, ycenter, lastxh, lastyh);
196     }
197 
198     @Override
199     public void start() {
200         timer = new Thread(this);
201         timer.start();
202     }
203 
204     @Override
205     public void stop() {
206         timer = null;
207     }
208 
209     @Override
210     @SuppressWarnings("SleepWhileHoldingLock")
211     public void run() {
212         Thread me = Thread.currentThread();
213         while (timer == me) {
214             try {
215                 Thread.sleep(100);
216             } catch (InterruptedException e) {
217             }
218             repaint();
219         }
220     }
221 
222     @Override
223     public String getAppletInfo() {
224         return "Title: A Clock \n"
225                 + "Author: Rachel Gollub, 1995 \n"
226                 + "An analog clock.";
227     }
228 
229     @Override
230     public String[][] getParameterInfo() {
231         String[][] info = {
232             { "bgcolor", "hexadecimal RGB number",
233                 "The background color. Default is the color of your browser." },
234             { "fgcolor1", "hexadecimal RGB number",
235                 "The color of the hands and dial. Default is blue." },
236             { "fgcolor2", "hexadecimal RGB number",
237                 "The color of the second hand and numbers. Default is dark gray." }
238         };
239         return info;
240     }
241 }